package com.ali.controller;

import com.ali.common.Response;
import com.ali.dto.OrderInfo;
import com.ali.service.OrderService;
import com.ali.service.SentinelService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private SentinelService sentinelService;

    /**
     * 查询
     */
    @GetMapping(value = "/query")
    public Response<OrderInfo> query(@RequestParam Map<String, Object> param) {
        return orderService.query(param);
    }

    /**
     * 控制
     */
    @GetMapping(value = "/control")
    public Response<String> control(@RequestParam Map<String, Object> param) {
        return orderService.control(param);
    }

    /**
     * Sentinel热点
     * SentinelResource：Sentinel资源
     */
    @SentinelResource(value = "hotspot")
    @GetMapping(value = "/sentinel/hotspot")
    public Response<String> hotspot(@RequestParam("num") int num, @RequestParam("num2") int num2) {
        return orderService.control(new HashMap<>() {{
            put("num", num);
            put("num2", num2);
        }});
    }

    /**
     * Sentinel授权
     */
    @GetMapping(value = "/sentinel/authorization")
    public Response<String> authorization() {
        return orderService.control(new HashMap<>());
    }

    /**
     * Sentinel资源
     */
    @GetMapping(value = "/sentinel/hello")
    public Response<String> hello(@RequestParam Map<String, Object> param) {
        return sentinelService.hello(param);
    }

    /**
     * Sentinel自定义服务限流和自定义服务降级
     */
    @GetMapping(value = "/sentinel/world")
    public Response<String> world(@RequestParam Map<String, Object> param) {
        return sentinelService.world(param);
    }
}